package com.wanggsx.library.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.wanggsx.library.util.fun.download.DownloadCallback;
import com.wanggsx.library.util.fun.download.DownloadManager;

import java.text.SimpleDateFormat;
import java.util.Date;

public class UtilsData {

    public static String getStrLimitLen(String str,int byteLen){
        int valueLength = 0;
        int count = 0;
        String chinese = "[\u4e00-\u9fa5]";
        for (int i = 0; i < str.length(); i++) {
            String temp = str.substring(i, i + 1);
            if (temp.matches(chinese)) {
                valueLength += 2;
            } else {
                valueLength += 1;
            }
            if (valueLength >= byteLen){
                break;
            }
            count++;
        }
        if (count == valueLength){
            return str;
        }
        return str.substring(0,count);
    }

    /** 返回字符串真实字节数 */
    public static int getStrLength(String value) {
        int valueLength = 0;
        String chinese = "[\u4e00-\u9fa5]";
        for (int i = 0; i < value.length(); i++) {
            String temp = value.substring(i, i + 1);
            if (temp.matches(chinese)) {
                valueLength += 2;
            } else {
                valueLength += 1;
            }
        }
        return valueLength;
    }

    /**
     * 判断字符串是否是数组中的某个元素
     * */
    public static boolean isInArr(String str, String[] arr){
        if (str==null || arr==null || arr.length==0){
            return false;
        }else{
            for (String str1 : arr){
                if (str.equals(str1)){
                    return true;
                }
            }
        }
        return false;
    }

    /** 将int转为十六进制字符串 */
    public static String intToHex(int n) {
        StringBuilder sb = new StringBuilder(8);
        String a;
        char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        while(n != 0){
            sb = sb.append(b[n%16]);
            n = n/16;
        }
        a = sb.reverse().toString();
        return a;
    }

    /** 接收以分为单位的整数，返回以元为单位的字符串，保留2位小数 */
    public static String getMoneyYuanStr(int moneyFen){
        String str;
        String strPrefix;
        if (moneyFen>=0){
            str = moneyFen + "";
            strPrefix = "";
        }else{
            str = moneyFen * -1 + "";
            strPrefix = "-";
        }
        if (str.length()==1){
            return strPrefix + "0.0" + str;
        }else if(str.length()==2){
            return strPrefix + "0." + str;
        }else{
            return strPrefix + str.substring(0,str.length()-2) + "." + str.substring(str.length()-2);
        }
    }

    /** 将以元为单位转换为以分为单位，去掉￥和元 */
    public static String getMoneyFenFromYuan(String strYuan){
        if (strYuan.length()>=4){
            String ret = strYuan.replace("￥","").replace("元","").replace(".","");
            if (ret.startsWith("0")){
                int len = 1;
                for (int i = 1;i<ret.length();i++){
                    if (ret.charAt(i)=='0'){
                        len++;
                    }else{
                        break;
                    }
                }
                ret = ret.substring(len);
            }
            return ret;
        }else{
            return "0";
        }
    }

    /***
     * 返回数组的字符串格式
     * @param arr
     * @param contactStr
     * @return
     */
    public static String getStringFromArr(int[] arr,String contactStr){
        if (arr!=null){
            StringBuilder sb = new StringBuilder();
            for (int i : arr){
                sb.append(contactStr + i);
            }
            return sb.toString().substring(1);
        }
        return null;
    }

    /** 接收以分为单位的整数，返回以元为单位的字符串，保留2位小数 */
    public static String getMoneyYuanStr(String moneyFen){
        if (UtilsString.isEmpty(moneyFen) || "null".equalsIgnoreCase(moneyFen)){
            return "0.00";
        }
        String str;
        String strPrefix;
        if (Integer.parseInt(moneyFen)>=0){
            str = moneyFen;
            strPrefix = "";
        }else{
            str = Integer.parseInt(moneyFen) * -1 + "";
            strPrefix = "-";
        }
        if (str.length()==1){
            return strPrefix + "0.0" + str;
        }else if(str.length()==2){
            return strPrefix + "0." + str;
        }else{
            return strPrefix + str.substring(0,str.length()-2) + "." + str.substring(str.length()-2);
        }
    }

    /**
     * 将字符串数组转为int数组
     * @param arr
     * @return
     */
    public static int[] getIntArrFromStringArr(String[] arr){
        if (arr!=null){
            int[] rtnArr = new int[arr.length];
            try{
                for (int i=0;i<arr.length;i++){
                    rtnArr[i] = Integer.parseInt(arr[i]);
                }
                return rtnArr;
            }catch (Exception e){

            }
        }
        return null;
    }

    /**
     * 对数组进行排序
     * @param nums
     * @param isAsc
     * @return
     */
    public static int[] getOrderedArr(int[] nums,boolean isAsc){
        for(int i=0;i<nums.length;i++){
            for(int j=i+1;j< nums.length;j++){
                if (isAsc){
                    if (nums[j]<nums[j-1]){
                        swipeArr(nums,j,j-1);
                    }
                }else{
                    if(nums[j]>nums[j-1]){
                        swipeArr(nums,j,j-1);
                    }
                }
            }
        }
        return nums;
    }

    /**
     * 交换数组中指定索引位的元素值
     * @param arr
     * @param index1
     * @param index2
     */
    public static void swipeArr(int[]arr,int index1,int index2){
        if (arr!=null && arr.length > index1 && arr.length > index2){
            int tmp = arr[index1];
            arr[index1] = arr[index2];
            arr[index2] = tmp;
        }
    }

    /** 获取等比例缩放后的屏幕，对应的宽度适配到屏幕宽度 */
    public static int getHeightScaledFitScreenWidth(int imageWidth,int imageHeight,int viewWidth){
        return imageHeight * viewWidth / imageWidth;
    }

    /** 让imageView控件适应等比例缩放的图片，宽度为指定宽度 */
    public static void resetViewHeightFitScaledImage(Context context, final ImageView imageView, String imageurl, final int imageviewWidth){
        String filePath = UtilsFile.getAutoFileDirectory() + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + ".jpg";
        DownloadManager.download(imageurl, filePath, true, new DownloadCallback() {
            @Override
            public void onProcess(int finishLen, int maxLen) {

            }

            @Override
            public void onFinish(boolean isSuccess, String fileName) {
                final Bitmap bitmap = BitmapFactory.decodeFile(fileName);
                final int height = getHeightScaledFitScreenWidth(bitmap.getWidth(),bitmap.getHeight(),imageviewWidth);
                imageView.post(new Runnable() {
                    @Override
                    public void run() {
                        ViewGroup.LayoutParams params = imageView.getLayoutParams();
                        if (params==null){
                            params = new ViewGroup.LayoutParams(imageviewWidth,height);
                            imageView.setLayoutParams(params);
                        }else{
                            params.width = UtilsScreen.getScreenWidth();
                            params.height = height;
                        }
                        imageView.setLayoutParams(params);
                        bitmap.recycle();
                    }
                });
            }
        });
    }

    /** 让imageView控件的高度自适应等比例缩放的ImageView，宽度为屏幕的宽度 */
    public static void resetViewHeightFitScreenWidth(Context context, String imageurl, final View viewGroup) {
        String filePath = UtilsFile.getAutoFileDirectory() + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + ".jpg";
        DownloadManager.download(imageurl, filePath, true, new DownloadCallback() {
            @Override
            public void onProcess(int finishLen, int maxLen) {

            }

            @Override
            public void onFinish(boolean isSuccess, String fileName) {
                final Bitmap bitmap = BitmapFactory.decodeFile(fileName);
                final int height = getHeightScaledFitScreenWidth(bitmap.getWidth(), bitmap.getHeight(), UtilsScreen.getScreenWidth());
                viewGroup.post(new Runnable() {
                    @Override
                    public void run() {
                        ViewGroup.LayoutParams params = viewGroup.getLayoutParams();
                        if (params == null) {
                            params = new ViewGroup.LayoutParams(UtilsScreen.getScreenWidth(), height);
                            viewGroup.setLayoutParams(params);
                        } else {
                            params.width = UtilsScreen.getScreenWidth();
                            params.height = height;
                        }
                        viewGroup.setLayoutParams(params);
                        bitmap.recycle();
                    }
                });
            }
        });
    }

}
